from loguru import logger
from functools import wraps
from sanic import Sanic
import pymysql
from datetime import datetime
from json import dumps


async def insert_access_logs(app,request):
    """middleware 接口访问信息插入数据库做记录

    :return:
    """

    access_url = request.url
    access_method = request.method
    access_host = request.host
    if access_method == 'GET':
        access_params_info = dumps(request.args)
    else:
        access_params_info = dumps(request.json)
    # access_params_info = pymysql.escape_string(str(access_params_info))
    access_time = datetime.now()
    # sql_insert_cmd = '''insert into access_logs(access_host,access_url, access_method, access_params_info, access_time,create_time) values('{}','{}','{}','{}','{}','{}')'''.format(
    #     access_host, access_url, access_method, access_params_info, access_time,datetime.now())

    sql_insert_cmd = '''insert into ifa.ifa_data_ext_access_logs(access_host,access_url, access_method, access_params_info, access_time,create_time) values('{}','{}','{}','{}','{}','{}')'''.format(
        access_host, access_url, access_method, access_params_info, access_time,datetime.now())
    # conn = app.config.MYSQL_DATA_APP.connection()
    conn = app.config.APP_DBPOOL.connection()
    cursor = conn.cursor()
    try:
        # 文件完成状态更新
        cursor.execute(sql_insert_cmd)
        # 调用中间服务API
    finally:
        conn.commit()
        cursor.close()
        conn.close()
    logger.info('@insert_access_logs --> 访问日志插入数据库！')


    # app=Sanic.get_app('__main__')
# def insert_access_logs(app):
#     """listener 接口访问信息插入数据库做记录
#
#     :return:
#     """
#     def decorator(f):
#         @wraps(f)
#         async def decorated_function(request, *args, **kwargs):
#             # run some method that checks the request
#             # for the client's authorization status
#             access_url = request.url
#             access_method = request.method
#             access_host = request.host
#             if access_method == 'GET':
#                 access_params_info = request.args
#             else:
#                 access_params_info = request.json
#             access_params_info = pymysql.escape_string(str(access_params_info))
#             logger.info(access_params_info)
#             access_time = datetime.now()
#             sql_insert_cmd = '''insert into access_logs(access_host,access_url, access_method, access_params_info, access_time) values('{}','{}','{}','{}','{}')'''.format(
#                 access_host, access_url, access_method, access_params_info, access_time)
#             conn = app.config.MYSQL_DATA_APP.connection()
#             cursor = conn.cursor()
#             try:
#                 # 文件完成状态更新
#                 cursor.execute(sql_insert_cmd)
#                 # 调用中间服务API
#             finally:
#                 conn.commit()
#                 cursor.close()
#                 conn.close()
#             logger.info('@insert_access_logs --> 访问日志插入数据库！')
#             response = await f(request, *args, **kwargs)
#             return response
#
#         return decorated_function
#     return decorator